package com.qiangesoft.rabbitmq.producer;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 消息发送配置
 *
 * @author qiangesoft
 * @date 2024-05-08
 */
@Configuration
public class MessageConfig {

    public static final String EXCHANGE = "demo.exchange";

    public static final String QUEUE = "demo.queue";

    public static final String ROUTING_KEY = "demo";

    @Bean
    public DirectExchange demoExchange() {
        return ExchangeBuilder
                .directExchange(EXCHANGE)
                // 持久化交换机
                .durable(true)
                .build();
    }

    @Bean
    public Queue demoQueue() {
        return QueueBuilder
                // 持久化队列
                .durable(QUEUE)
                // 避免消息堆积、懒加载
                .lazy()
                .build();
    }

    @Bean
    public Binding demoBinding(Queue demoQueue, DirectExchange demoExchange) {
        return BindingBuilder.bind(demoQueue).to(demoExchange).with(ROUTING_KEY);
    }

}
